﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace SuperCalculator
{
    internal class Function
    {
        public static readonly string FunctionNameSpace = "FunctionNameSpace";
        public static readonly string FunctionClassName = "FunctionClass";

        /// <summary>
        /// using文件(.us)的备份。
        /// </summary>
        public static string UsingFileBackup = null;

        /// <summary>
        /// 保存目前所有的函数信息
        /// </summary>
        public static FunctionInfoCollection CollectionOfFunctionInfo { get; set; } = new FunctionInfoCollection();


        /// <summary>
        /// 保存所有函数相关文件的文件夹, 如果不存在则创建。
        /// </summary>
        public static string DirectoryOfFunctions
        {
            get
            {
                var dir = Application.StartupPath + Path.DirectorySeparatorChar + "Functions";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                return dir;
            }
        }

        /// <summary>
        /// 保存函数列表的列表文件的完整路径, 如果不存在则创建。
        /// </summary>
        public static string PathOfFunctionsListFile
        {
            get
            {
                var path = DirectoryOfFunctions + Path.DirectorySeparatorChar + "FuncionsList.xml";
                if (!File.Exists(path))
                {
                    File.Create(path);
                }
                return path;
            }
        }
        
        /// <summary>
        /// 获取函数dll的完整路径, 该路径不一定存在。
        /// </summary>
        public static string GetPathOfFunctionDll()
        {
            return DirectoryOfFunctions + Path.DirectorySeparatorChar + "Functions.dll";
        }

        /// <summary>
        /// 获取最新的函数dll的完整路径, 该路径不一定存在。
        /// </summary>
        public static string GetPathOfNewFunctionDll()
        {
            var dir = DirectoryOfFunctions + Path.DirectorySeparatorChar + "New_FunctionsDll";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return dir + Path.DirectorySeparatorChar + "Functions.dll";
        }

        /// <summary>
        /// 获取函数代码文件的相对路径, 该路径不一定存在。(如果要使用绝对路径, 请在前面加上Function.DirectoryOfFunctions)
        /// </summary>
        /// <param name="functionName">函数名。</param>
        public static string GetRelativePathOfFunctionCodeFile(string functionName)
        {
            return Path.DirectorySeparatorChar + functionName + ".cf";
        }

        /// <summary>
        /// 获取保存所有using语句的文本文件, 如果不存在则创建。
        /// </summary>
        public static string GetPathOfUsingFile()
        {
            var path = DirectoryOfFunctions + Path.DirectorySeparatorChar + "using.us";
            if (!File.Exists(path))
            {
                var sw = new StreamWriter(path, false, Encoding.Default);
                try
                {
                    if (string.IsNullOrEmpty(UsingFileBackup))
                    {
                        sw.WriteLine("using System;");
                    }
                    else
                    {
                        sw.Write(UsingFileBackup);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error at GetPathOfUsingFile(): \r\n" + ex);
                }
                finally
                {
                    sw.Close();
                }
            }
            return path;
        }

        /// <summary>
        /// 获取保存名字空间和类头语句的文本文件(.ch), 如果不存在则创建.
        /// </summary>
        public static string GetPathOfClassHeaderFile()
        {
            var path = DirectoryOfFunctions + Path.DirectorySeparatorChar + "classHeader.ch";

            if (!File.Exists(path))
            {
                var sw = new StreamWriter(path, false, Encoding.Default);
                try
                {
                    var s =
                        Environment.NewLine +
                        "namespace " + FunctionNameSpace + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "   public class " + FunctionClassName + Environment.NewLine +
                        "   {" + Environment.NewLine;
                    sw.WriteLine(s);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error at GetPathOfClassHeaderFile(): \r\n" + ex);
                }
                finally
                {
                    sw.Close();
                }
            }
            return path;
        }

        /// <summary>
        /// 获取保存名字空间和类尾语句的文本文件(.ct), 如果不存在则创建。
        /// </summary>
        public static string GetPathOfClassTailFile()
        {
            var path = DirectoryOfFunctions + Path.DirectorySeparatorChar + "classTail.ct";

            if (!File.Exists(path))
            {
                var sw = new StreamWriter(path, false, Encoding.Default);
                try
                {
                    var s =
                        Environment.NewLine +
                        "   }//class_end " + Environment.NewLine +
                        "}//namespace_end" + Environment.NewLine;
                    sw.WriteLine(s);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error at GetPathOfClassHeaderFile(): \r\n" + ex);
                }
                finally
                {
                    sw.Close();
                }
            }
            return path;
        }

        /// <summary>
        /// 序列化函数到列表。
        /// </summary>
        public static bool SaveFunctionsToList()
        {
            var listPath = PathOfFunctionsListFile;
            var sw = new StreamWriter(listPath, false);
            var serialiser = new XmlSerializer(typeof(FunctionInfoCollection));

            try
            {
                serialiser.Serialize(sw, CollectionOfFunctionInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("序列化函数到列表时出错: \r\n" + ex);
                return false;
            }
            finally
            {
                sw.Close();
            }
            return true;
        }
    }
}
